package com.test.prolenders.adapter_prolenders;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.test.prolenders.R;
import com.test.prolenders.bean_prolenders.config_prolenders.AccountChannel_Fr_prolenders;
import com.test.prolenders.util_prolenders.http_prolenders.HttpUtil_prolenders;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


public class AccountChannelViewAdapter_prolenders extends RecyclerView.Adapter<AccountChannelViewAdapter_prolenders.AccountChannelHolder> {

    private final ArrayList<AccountChannel_Fr_prolenders> channels;

    private final String page;

    private AccountChannel_Fr_prolenders selectChannel;

    private final ArrayList<AccountChannelHolder> holderList = new ArrayList<>();

    public AccountChannelViewAdapter_prolenders(ArrayList<AccountChannel_Fr_prolenders> channels,String page){
        this.channels = channels;
        this.page = page;
    }

    public AccountChannel_Fr_prolenders getSelectChannel() {
        return selectChannel;
    }

    @NonNull
    @Override
    public AccountChannelHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AccountChannelHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_account_channel_fr_prolenders,parent,false));
    }

    @SuppressLint("InflateParams")
    @Override
    public void onBindViewHolder(@NonNull AccountChannelHolder holder, int position) {

        if (!holderList.contains(holder)) holderList.add(holder);

        AccountChannel_Fr_prolenders channel = channels.get(position);

        holder.setChannelDesc(channel.getDesc());

        holder.setClick(v -> {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"DIALOG_TYPE_CLICK",channel.getDesc());
            selectChannel = channel;
            for (AccountChannelHolder holder1:holderList){
                holder1.setCheck(holder1 == holder);
            }
        });

        //获取图标
        if (channel.getBitmap()==null){
            new Thread(() -> {
                try {
                    if (TextUtils.isEmpty(channel.getLogo())) return;
                    URL url = new URL(channel.getLogo());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        channel.setBitmap(bitmap);
                        new Handler(Looper.getMainLooper()).post(() -> holder.setLogo(bitmap));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }else holder.setLogo(channel.getBitmap());

    }

    @Override
    public int getItemCount() {
        return channels.size();
    }

    public static class AccountChannelHolder extends RecyclerView.ViewHolder{

        private final View clickView;
        private final ImageView checkIv;
        private final ImageView logoIv;
        private final TextView channelTv;

        private final int colorBlue,colorGray;

        @SuppressLint("InflateParams")
        public AccountChannelHolder(View view){

            super(view);

            clickView = view.findViewById(R.id.iacf_view_click);
            checkIv = view.findViewById(R.id.iacf_iv_check);
            logoIv = view.findViewById(R.id.iacf_iv_logo);
            channelTv = view.findViewById(R.id.iacf_tv_channel);

            colorBlue = view.getContext().getColor(R.color.blue_main_prolenders);
            colorGray = Color.parseColor("#FF6E6E6E");

        }

        public void setClick(View.OnClickListener clickListener){
            if (clickView!=null) clickView.setOnClickListener(clickListener);
        }

        public void setCheck(boolean isCheck){
            if (isCheck){
                checkIv.setBackgroundResource(R.drawable.icon_check_true_prolenders);
                clickView.setBackgroundResource(R.drawable.bg_blue_t_rc11dp_border_blue_1dp_prolenders);
                channelTv.setTextColor(colorBlue);
            }else {
                checkIv.setBackgroundResource(R.drawable.icon_check_false_prolenders);
                clickView.setBackgroundResource(R.drawable.bg_gray_rc11dp_prolenders);
                channelTv.setTextColor(colorGray);
            }
        }

        public void setLogo(Bitmap bitmap){
            logoIv.setImageBitmap(bitmap);
        }

        public void setChannelDesc(String text){
            channelTv.setText(text);
        }



    }
}
